package com.xueqiu.lovepetmedicalmanagersystem.controller;

import com.xueqiu.lovepetmedicalmanagersystem.pojo.Pets;
import com.xueqiu.lovepetmedicalmanagersystem.pojo.Visits;
import com.xueqiu.lovepetmedicalmanagersystem.service.PetService;
import com.xueqiu.lovepetmedicalmanagersystem.service.VisitsService;
import com.xueqiu.lovepetmedicalmanagersystem.util.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import java.util.Date;

/**
 * @author xueqiu
 */
@Controller
@RequestMapping("/visit")
public class VisitsController {

    @Autowired
    private VisitsService visitsService;
    @Autowired
    private PetService petService;

    /**
     * 宠物病历新增
     * @param visits 数据源
     * @param model 模型参数
     * @return 新增成功则跳转到宠物更新页面；新增失败则跳转到宠物病历新增页面
     */
    @RequestMapping("/addVisit")
    public String addVisit(Visits visits, Model model){
        Date date = new Date();
        visits.setVisitDate(date);

        Integer i = visitsService.addVisit(visits);

        if (i > 0) {
            Pets pet = petService.getPetByName(visits.getPetName());
            model.addAttribute("pet", pet);
            model.addAttribute("petBirthDate", DateUtils.DateToStr(pet.getBirthDate()));
            model.addAttribute("msg", "宠物病历添加成功！！！");
            return "pet/petupdate";
        } else {
            model.addAttribute("msg", "宠物病历添加失败！！！");
            return "pet/addvisit";
        }
    }
}
